package com.sheep.jucdemo.part1.part1_3;


/**
 * @Date 2022/3/28 23:05
 * @Created by sheep
 */
public class Test1_3_5 {

    private static volatile Object resourceA = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    System.out.println("threadA get resourceA lock");

                    try {
                        System.out.println("ThreadA begin wait");
                        resourceA.wait();
                        System.out.println("ThreadA end wait");
                        System.out.println("ThreadA end wait");
                        System.out.println("ThreadA end wait");
                        System.out.println("ThreadA end wait");
                        System.out.println("ThreadA end wait");
                        System.out.println("ThreadA end wait");
                        System.out.println("ThreadA end wait");
                        System.out.println("ThreadA end wait");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    System.out.println("threadB get resourceA lock");
                    try {
                        System.out.println("ThreadB begin wait");
                        resourceA.wait();
                        System.out.println("ThreadB end wait");
                        System.out.println("ThreadB end wait");
                        System.out.println("ThreadB end wait");
                        System.out.println("ThreadB end wait");
                        System.out.println("ThreadB end wait");
                        System.out.println("ThreadB end wait");
                        System.out.println("ThreadB end wait");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                /*try {
                    System.out.println("ThreadB begin wait");
                    // 如果该线程没有获取到共享对象的监视器锁，才可以调用notify()方法，否则会抛出IllegalMonitorStateException异常
                    resourceA.wait();
                    System.out.println("ThreadB end wait");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            }
        });
        Thread threadC = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA) {
                    System.out.println("ThreadC begin notify");
                    resourceA.notifyAll();
//                    resourceA.notify();
                }
            }
        });

        threadA.start();
        threadB.start();
        //主线程睡眠1s确保threadA、threadB执行到wait()方法后threadC再调用notify()方法
        Thread.sleep(1000);
        threadC.start();

        //等待线程结束
        threadA.join();
        threadB.join();
        threadC.join();
    }
}
